package cn.caipengbo.helloandroid;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

    }
    // 使用普通 Alert Dialog ——  方法链调用
    public void showAd(View view) {
        new AlertDialog.Builder(this)
                .setTitle("AlertDialog标题")
                .setMessage("这是里面的Message")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    //
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }
    // 列表  、 单选 AlertDialog
    public void showListDialog(View view) {
        final String[] items = {"红", "蓝", "绿", "白"};
        new AlertDialog.Builder(this)
                .setTitle("选择对话框")
                .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                    // 注意
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
                        // 窗口关闭
                        dialog.dismiss();
                    }
                })
                .show();
    }
    //  自定义AlertDiaglog 自定义Dialog的布局文件
    public void showCustomizeDialog(View view) {
        // 先写布局文件 —— dialog_view.xml
        // View 与 布局文件关联
        View view1 = View.inflate(this, R.layout.dialog_view, null);

        final EditText editTextUsername = (EditText) view1.findViewById(R.id.edit_username);
        final EditText editTextPassword = (EditText) view1.findViewById(R.id.edit_password);
        new AlertDialog.Builder(this)
                .setView(view1)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String username = editTextUsername.getText().toString();
                        String password = editTextPassword.getText().toString();
                        Toast.makeText(DialogActivity.this, username + password, Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }
    // 显示圆形进度的Alert Dialog
    public void showProgressDialog(View view) {
        // 主线程Progress Dialog
        // ProgressDialog.show(this, "进度Dialog标题", "这是信息...")
        final ProgressDialog progressDialog = ProgressDialog.show(this, "进度Dialog标题", "这是信息...");
        // 子线程进行
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                progressDialog.dismiss();
                // 不能在子线程中更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DialogActivity.this, "这是在主线程执行的哦!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();

    }
    // 水平进度条 Alert Dialog
    public void showProgressDialog2(View view) {
        // 注意观察与 上一个方法 创建圆形进度
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
        int count = 20;
        // 关于进度条的用法都差不多
        progressDialog.setMax(count);
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progressDialog.setProgress(progressDialog.getProgress() + 1);
                }
                progressDialog.dismiss();
                // 不能在子线程中更新UI
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DialogActivity.this, "这是在主线程执行的哦!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
    }
    // 使用 日期 对话框 选择日期
    public void showDatePicker(View view) {
        // 使用 java.util.Calendar 获得时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        new DatePickerDialog(DialogActivity.this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Log.d(new Integer(year).toString() + " " + new Integer(month).toString()
                        + " " + new Integer(dayOfMonth).toString(), "onDateSet: ");
            }
        }, year, month, day).show();
    }
    // 使用 时间对话框选择时间
    public void showTimePicker(View view) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        new TimePickerDialog(DialogActivity.this, new TimePickerDialog.OnTimeSetListener(){

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                String strHour = new Integer(hourOfDay).toString();
                String strMinute = new Integer(minute).toString();
                Log.d("---" + strHour + " : " + strMinute + "---", "onDateSet: ");
            }
        }, hour, minute, true).show();
    }
}
